<?php

if (!function_exists('getip')) {
    function getip()
    {
        static $ip = '';
        $ip = $_SERVER['REMOTE_ADDR'];
        if (isset($_SERVER['HTTP_CDN_SRC_IP'])) {
            $ip = $_SERVER['HTTP_CDN_SRC_IP'];
        } elseif (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } elseif (isset($_SERVER['HTTP_X_FORWARDED_FOR']) && preg_match_all('#\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}#s', $_SERVER['HTTP_X_FORWARDED_FOR'], $matches)) {
            foreach ($matches[0] AS $xip) {
                if (!preg_match('#^(10|172\.16|192\.168)\.#', $xip)) {
                    $ip = $xip;
                    break;
                }
            }
        }
        if (preg_match('/^([0-9]{1,3}\.){3}[0-9]{1,3}$/', $ip)) {
            return $ip;
        } else {
            return '127.0.0.1';
        }
    }
}

if (!function_exists('verify_code')) {
    /**
     * 获取4位验证码
     */
    function verify_code(){
        $randStr = str_shuffle('1234567890');
        $rand = substr($randStr,0,4);
        return $rand;
    }
}

if (!function_exists('sms_data')) {
    function sms_data($smstype, $data)
    {
        $result = '';
        if ($smstype == 1) {
            $count = count($data);
            $i = 0;
            foreach ($data as $key => $value) {
                if ((0 < $i) && ($i < $count)) {
                    $result .= '&';
                }
                $result .= '#' . $key . '#=' . $value;
                ++$i;
            }
            $result = urlencode($result);
        }
        if ($smstype == 2 || $smstype == 3) {
            $result = json_encode($data);
        }
        return $result;
    }
}

if (!function_exists('wgTomedia')) {
    function wgTomedia($src){
        $remote = \App\Services\ConfigService::getCoreValueByType('remote');
        $attach_url = $remote['alioss']['url'] . '/';
        return $attach_url . $src;
    }
}

if (!function_exists('exception')) {
    /**
     * 抛出异常处理
     *
     * @param string $msg 异常消息
     * @param integer $code 异常代码 默认为0
     * @param string $exception 异常类
     *
     * @throws Exception
     */
    function exception($msg, $code = 0, $exception = '')
    {
        $e = $exception ?: \Exception::class;
        throw new $e($msg, $code);
    }
}

if (!function_exists('exception')) {
    /**
     * 抛出异常处理
     *
     * @param string $msg 异常消息
     * @param integer $code 异常代码 默认为0
     * @param string $exception 异常类
     *
     * @throws Exception
     */
    function exception($msg, $code = 0, $exception = '')
    {
        $e = $exception ?: \Exception::class;
        throw new $e($msg, $code);
    }
}

if (!function_exists('getNumberColor')) {
    /**
     * 获取数字文本颜色
     * @param $number
     */
    function getNumberColor($number)
    {
        if ($number >= 0) {
            return 'text-danger';
        } else {
            return 'text-primary';
        }
    }
}

if(!function_exists('http_get')){
    /**
     * @brief                  get请求
     * @param string $url       请求的url
     * @param array $param     请求的参数
     * @param int $timeout     超时时间
     * @return mixed
     */
    function get($url, $param=array(), $timeout=10)
    {
        $ch = curl_init();
        if (is_array($param)) {
            $url = $url . '?' . http_build_query($param);
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); // 允许 cURL 函数执行的最长秒数
        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}

if(!function_exists('http_post')){
    /**
     * @brief                   post请求
     * @param string $url              请求的url地址
     * @param array $param      请求的参数
     * @param int $log          是否启用日志
     * @return mixed
     */
    function http_post($url, $param=array(), $isJson=0, $timeout=10)
    {
        $ch = curl_init();
        if (is_array($param)) {
            $urlparam = http_build_query($param);
        } else if (is_string($param)) { //json字符串
            $urlparam = $param;
        }
        if($isJson){
            $urlparam = json_encode($param);
            curl_setopt($ch, CURLOPT_HTTPHEADER,  ["Content-Type: application/json"] );
        }
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //设置超时时间
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回原生的（Raw）输出
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_POST, 1); //POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, $urlparam); //post数据

        $data = curl_exec($ch);
        curl_close($ch);
        return $data;
    }
}
